
import { deleteRequest, error, getRequest, ok, postRequest, putRequest } from "../utils/myAxios.ts";
import { Course, Res } from "../types";

// 获取课程列表
export async function getCourseList(): Promise<Res<Array<Course>>> {
    const list = await getRequest<Array<Course>>(`/course`);
    console.log('获取课程列表', list);
    if (list?.length >= 0) {
        return ok(list, 'ok');
    } else {
        return error([],"请求失败，请重试");
    }
}

// 添加课程
export async function addCourse(course: Course): Promise<Res<Course>> {
    const r = await postRequest<Course>(`/course`, course);
    console.log('添加课程：', r);
    if (r.id) {
        return ok(r, '添加成功');
    } else {
        return error(r);
    }
}

// 更新课程信息
export async function updateCourse(course: Course): Promise<Res<Course>> {
    const r = await putRequest<Course>(`/course/${course.id}`, course);
    console.log('更新课程res：', r);
    if (r.id) {
        return ok(r, '更新成功');
    } else {
        return error(r);
    }
}

// 删除课程
export async function deleteCourse(id: string): Promise<Res<Course>> {
    const r = await deleteRequest<Course>(`/course/${id}`);
    console.log('删除课程res：', r);
    if (r.id) {
        return ok(r, '删除成功');
    } else {
        return error(r);
    }
}

// 查询单个课程
export async function getCourseById(id: string): Promise<Res<Course>> {
    const r = await getRequest<Course>(`/course/${id}`);
    console.log('查询课程res：', r);
    if (r.id) {
        return ok(r, '查询成功');
    } else {
        return error(r);
    }
}